<template>
    <div>
        <el-row>
            <el-col :span="24">
                <el-form ref="form" :rules="rules" :model="form" label-width="100px">
                    <el-form-item label="设备码" prop="uuid">
                        <el-input v-model="form.uuid" placeholder="设备码"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="备注"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" :loading="loading" @click="submit">确定</el-button>
                        <el-button @click="cancel">返回</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
        props: {
            data: {

            }
        },
        data() {
            return {
                loading: false,
                form: {},
                formDefault: {
                    uuid: '',
                    remark: '',
                },
                rules: {
                    uuid: [
                        { required: true, message: '请输入设备号', trigger: 'blur' },
                    ],
                    remark: [
                        { required: true, message: '请输入备注', trigger: 'blur' },
                    ],
                },
                // options: this.$store.state.appSource.options,
            }
        },
        computed: {},
        watch: {
            data (newVal) {
                this.syncForm();
            }
        },
        methods: {
            getFormAction() {
                return this.form.id ? 'uuidWhiteList/Update' : 'uuidWhiteList/Store';
            },
            submit() {
                this.$refs.form.validate(valid => {
                    if (!valid) return false;
                    this.loading = true;
                    this.$store.dispatch(this.getFormAction(), this.form).then(res => {
                        this.$message.success(res.data.message);
                        this.$emit('submit');
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            cancel() {
                this.$emit('cancel');
            },
            syncForm() {
                this.form = Object.assign({}, this.data.id ? this.data : this.formDefault);
            }
        },
        created () {
            this.syncForm();
        }
    }
</script>

<style lang="scss" scoped>
.item-row {
    margin: 0 0 10px 0;
}
</style>

